﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using System.Data.OleDb;

namespace QuanLiHocSinh
{
    public partial class LapDanhSachLop : Form
    {
        OleDbConnection connect;
        public LapDanhSachLop()
        {
            InitializeComponent();

            //===========================
            connect = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=CSDL.mdb");

            if (connect != null)
            {
                connect.Open();


                string point = "select * from lop";
                OleDbCommand comman = new OleDbCommand(point, connect);

                OleDbDataReader reader;
                reader = comman.ExecuteReader();

                while (reader.Read())
                {

                    string lop = "";
                    lop += reader.GetString(0);

                    comboBox2.Items.Add(lop);
                }

                
            }
            else
            {
                MessageBox.Show("Không kết nối được với CSDL", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
            //===========================

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            int num = 0;
            string get = "select count(*) from hocsinh where Lop = '" +comboBox2.Text+ "'";
            OleDbCommand comman = new OleDbCommand(get, connect);

            OleDbDataReader reader = comman.ExecuteReader();
            while(reader.Read())
            {
                num = reader.GetInt32(0);
            }

            textBox1.Text = num.ToString();
            int i=0;
            int stt = 1;
            DateTime ngaysinh;
            get = "select * from hocsinh where Lop = '" + comboBox2.Text + "'";
            comman = new OleDbCommand(get, connect);
            reader = comman.ExecuteReader();
            while (reader.Read())
            {
                listView1.Items.Add(stt.ToString());
                listView1.Items[i].SubItems.Add(reader.GetString(1));
                listView1.Items[i].SubItems.Add(reader.GetString(2));
                ngaysinh = reader.GetDateTime(3);
                listView1.Items[i].SubItems.Add(ngaysinh.ToString());
                listView1.Items[i].SubItems.Add(reader.GetString(4));
                stt++;
                i++;

            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            PrintDialog pr = new PrintDialog();
            
            pr.ShowDialog();
           
        }


    }
}
